﻿Imports System.Xml
Imports System.Reflection
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Workflow.BusinessDomain
Imports Mysoft.Map.Data
Imports System.Web

Public Class YearPlanSP

    Inherits BusinessDomainBase

    Private _businessGUID As String     '业务GUID
    Private _xml As XmlDocument = New XmlDocument

    Public Sub New()
        MyBase.New("/PubProject/workflow/qmys/YearPlanSP.xml")
        _xml.LoadXml(MyBase.XML)
    End Sub

    '属性：业务GUID
    Public Overrides Property BusinessGUID() As String
        Get
            Return _businessGUID
        End Get
        Set(ByVal Value As String)
            _businessGUID = Value
        End Set
    End Property

    '属性：XML
    '最终XML格式：

    Public Overrides Property XML() As String
        Get
            Return _xml.OuterXml
        End Get
        Set(ByVal Value As String)
            _xml.LoadXml(Value)
        End Set
    End Property

    '功能：取业务数据，填充XML
    Public Overrides Function LoadData() As Integer
        Dim strSQL, strYearPlanGUID, strBUGUID, strBUType As String
        Dim dtTemp As DataTable

        '主记录    
        strSQL = "SELECT c.BUGUID,c.BUName,b.YearPlanGUID,b.PlanName,b.PlanYear,Convert(Varchar(10), GetDate(), 120) AS ReportDate" & _
                 " FROM ys_YearPlan2Object a " & _
                 " INNER JOIN ys_YearPlan b ON a.YearPlanGUID=b.YearPlanGUID " & _
                 " INNER JOIN myBusinessUnit c ON a.ObjectGUID=c.BUGUID " & _
                 " WHERE a.YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then
            strBUGUID = dtTemp.Rows(0).Item("BUGUID").ToString
            strYearPlanGUID = dtTemp.Rows(0).Item("YearPlanGUID").ToString
            '根节点业务GUID属性
            _xml.SelectSingleNode("/BusinessType").Attributes.GetNamedItem("BusinessGUID").Value = Me._businessGUID
            '_xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度预算计划2部门GUID']").InnerText = dtTemp.Rows(0).Item("YearPlan2ObjectGUID").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='公司GUID']").InnerText = strBUGUID
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='公司名称']").InnerText = dtTemp.Rows(0).Item("BUName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='版本GUID']").InnerText = strYearPlanGUID
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='版本名称']").InnerText = dtTemp.Rows(0).Item("PlanName").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='年度']").InnerText = dtTemp.Rows(0).Item("PlanYear").ToString
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报日期']").InnerText = dtTemp.Rows(0).Item("ReportDate").ToString

            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='上报人名称']").InnerText = HttpContext.Current.Session("UserName").ToString()

            '获取公司属性，根据公司的不同类型采用不同的方法取数
            strSQL = "SELECT Case When Level = 0 Then '0' When IsEndCompany = 1 Then '1' Else '2' End As BUType FROM myBusinessUnit WHERE BUGUID = '" & strBUGUID & "'"
            strBUType = MyDB.GetDataItemString(strSQL)

            If strBUType = "1" Then '末级公司情况
                strSQL = " SELECT ROUND(isnull(sum(PlanAmountYear),0),2) as PlanAmountYear" & _
                         " FROM ys_YearPlan2Object a where ItemType in ('部门费用','项目成本','项目税费') AND YearPlanGUID='" & strYearPlanGUID & "' AND BUGUID='" & strBUGUID & "'"
            Else '集团或区域公司情况
                strSQL = " SELECT ROUND(isnull(sum(PlanAmountYear),0),2) as PlanAmountYear" & _
                         " FROM ys_YearPlan2Object a " & _
                         " INNER JOIN vys_myBusinessUnit b ON a.ObjectGUID=b.BUGUID" & _
                         " where ItemType in ('部门费用','公司支出') AND YearPlanGUID='" & strYearPlanGUID & "' AND a.ObjectGUID !='" & strBUGUID & "' AND  (b.ParentGUID='" & strBUGUID & "' OR b.CompanyGUID='" & strBUGUID & "') "
            End If
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='支出合计']").InnerText = Format(MyDB.GetDataItemDecimal(strSQL), "#,##0.00")
            
            If strBUType = "1" Then '末级公司情况
                strSQL = " SELECT ROUND(isnull(sum(PlanAmountYear),0),2) as PlanAmountYear" & _
                         " FROM ys_YearPlan2Object a where ItemType='销售收入' AND YearPlanGUID='" & strYearPlanGUID & "' AND BUGUID='" & strBUGUID & "'"
            Else '集团或区域公司情况
                strSQL = " SELECT ROUND(isnull(sum(PlanAmountYear),0),2) as PlanAmountYear" & _
                         " FROM ys_YearPlan2Object a " & _
                         " INNER JOIN vys_myBusinessUnit b ON a.ObjectGUID=b.BUGUID" & _
                         " where ItemType ='公司收入' AND YearPlanGUID='" & strYearPlanGUID & "' AND a.ObjectGUID !='" & strBUGUID & "' AND (b.IfEnd=1 OR b.IsCompany=1) AND  (b.ParentGUID='" & strBUGUID & "' OR b.CompanyGUID='" & strBUGUID & "') "
            End If
            _xml.SelectSingleNode("/BusinessType/Item/Domain[@name='收入合计']").InnerText = Format(MyDB.GetDataItemDecimal(strSQL), "#,##0.00")
        End If
        Return 0
    End Function

    '功能：发起审批流程
    Public Overrides Function Initiate() As String
        Dim strSQL, strYearPlanGUID, strBUGUID, strUserGUID, strUserName As String
        Dim dtTemp As DataTable

        strSQL = "SELECT a.YearPlanGUID,a.ObjectGUID as BUGUID" & _
                 " FROM ys_YearPlan2Object a " & _
                 " WHERE a.YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then

            strBUGUID = dtTemp.Rows(0).Item("BUGUID").ToString  '上报公司GUID
            strYearPlanGUID = dtTemp.Rows(0).Item("YearPlanGUID").ToString '上报版本GUID
            strUserName = HttpContext.Current.Session("UserName").ToString() '上报人名称
            strUserGUID = HttpContext.Current.Session("UserGUID").ToString() '上报人GUID

            Dim cYearPlanMng As YearPlanMng = New YearPlanMng()

            If cYearPlanMng.ReportYearPlanCompany(strYearPlanGUID, strBUGUID, strUserGUID, strUserName) Then

                Return "<xml result=""true"" errormessage=""""></xml>"
            Else
                Return "<xml result=""false"" errormessage=""上报失败，请稍后再试！""></xml>"
            End If
        Else
            Return "<xml result=""false"" errormessage=""上报失败，请稍后再试！""></xml>"
        End If
    End Function
   

    '功能：根据XML进行数据归档
    Public Overrides Function SaveData() As String
        Dim MyPublicMng As New PublicMNG
        Dim strSQL, strYearPlanGUID, strBUGUID, strUserName As String
        Dim dtTemp As DataTable
        '获取审核人、审核日期的审核抬头信息
        Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator("审批通过")

        '求出要上报的公司GUID和版本GUID
        strSQL = "SELECT a.YearPlanGUID,a.ObjectGUID as BUGUID" & _
                 " FROM ys_YearPlan2Object a " & _
                 " WHERE a.YearPlan2ObjectGUID='" & Me._businessGUID & "'"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count > 0 Then

            strBUGUID = dtTemp.Rows(0).Item("BUGUID").ToString
            strYearPlanGUID = dtTemp.Rows(0).Item("YearPlanGUID").ToString
            '根据公司GUID和版本GUID将公司收入和公司支出的状态改为已审核
            strSQL = "UPDATE ys_YearPlan2Object SET ItemState='已审核'" & _
                       ",ApproveRemarks='" & strApproveSeparator & Chr(13) & Chr(10) & "工作流审批通过" & Chr(13) & Chr(10) & Chr(13) & Chr(10) & "' + Convert(varchar(6000), IsNull(ApproveRemarks, '')) " & _
                       " WHERE ItemType in ('公司收入','公司支出') AND ObjectGUID = '" & strBUGUID & "' AND YearPlanGUID ='" & strYearPlanGUID & "'"

            If MyDB.ExecSQL(strSQL) > 0 Then
                Return "<xml result=""true"" errormessage=""""></xml>"

            End If

        End If

        Return "<xml result=""false"" errormessage=""归档失败，请稍后再试！""></xml>"
    End Function

    '功能：不通过处理
    Public Overrides Function SaveData_NoPass() As String
        
        Return CancelCompanyYearPlan("审批不通过", "工作流审批不通过")
    End Function

    '功能：审批作废处理
    Public Overrides Function SaveData_Cancel() As String
        Return CancelCompanyYearPlan("审批不通过", "工作流审批不通过")
    End Function

    '功能：审批通过后再驳回数据处理
    Public Function SaveData_RollBack() As String

        Return CancelCompanyYearPlan("审批驳回", "工作流审批驳回")
    End Function

    '功能：发起撤回
    Public Overrides Function InitiateWithDrawn() As String

        Return CancelCompanyYearPlan("审批不通过", "工作流审批不通过")
    End Function

#Region "私有方法"
    '取消公司的年度预算
    Private Function CancelCompanyYearPlan(ByVal strActiveName As String, ByVal strApproveRemarks As String) As String
        Dim MyPublicMng As New PublicMNG
        Dim strSQL, strSaleYearPlan2ObjectGUID, strExpenseYearPlan2ObjectGUID As String
        Dim dtTemp As DataTable
        Dim i As Integer
        '获取审核人、审核日期的审核抬头信息
        'Dim strApproveSeparator As String = MyPublicMng.GetApproveSeparator()

        '取出上报公司的公司收入和公司支出的YearPlan2ObjectGUID
        strSQL = "SELECT a.YearPlan2ObjectGUID,ItemType" & _
                 " FROM ys_YearPlan2Object a" & _
                 " WHERE a.ObjectGUID=(SELECT ObjectGUID FROM ys_YearPlan2Object b WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "')" & _
                 " AND a.YearPlanGUID=(SELECT YearPlanGUID FROM ys_YearPlan2Object b WHERE YearPlan2ObjectGUID='" & Me._businessGUID & "')"

        dtTemp = MyDB.GetDataTable(strSQL)

        If dtTemp.Rows.Count >= 2 Then

            For i = 0 To dtTemp.Rows.Count - 1

                If Convert.ToString(dtTemp.Rows(i).Item("ItemType")) = "公司收入" Then
                    strSaleYearPlan2ObjectGUID = Convert.ToString(dtTemp.Rows(i).Item("YearPlan2ObjectGUID"))
                ElseIf Convert.ToString(dtTemp.Rows(i).Item("ItemType")) = "公司支出" Then
                    strExpenseYearPlan2ObjectGUID = Convert.ToString(dtTemp.Rows(i).Item("YearPlan2ObjectGUID"))
                Else

                End If
            Next

            Dim cYearPlanMng As YearPlanMng = New YearPlanMng()
            '将公司收入和公司支出的YearPlan2ObjectGUID组成字符串一起取消审核
            If cYearPlanMng.YearPlanApproveUnPass(strSaleYearPlan2ObjectGUID + "," + strExpenseYearPlan2ObjectGUID, strApproveRemarks, strActiveName) Then

                Return "<xml result=""true"" errormessage=""""></xml>"
            End If

        End If

        Return "<xml result=""false"" errormessage=""操作失败，请稍后再试！""></xml>"

    End Function

#End Region

End Class
